#define _CRT_SECURE_NO_WARNINGS
#include"queue.h"


void Queueinit(queue* p) {
	assert(p);
	p->head = p->tail = NULL;
	p->size = 0;
}


void Queuedestroy(queue* p) {
	assert(p);
	Qnode* cur = p->head;
	while (cur) {
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
	p->size = 0;
	p->tail = p->head = NULL;
}


void Queuebackpush(queue* p, qdatatype x) {
	assert(p);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	if (newnode == NULL) {
		perror(malloc);
		return;
	}
	newnode->next = NULL;
	newnode->data = x;
	if (p->tail == NULL) {
		p->head = p->tail = newnode;
	}
	else {
		p->tail->next = newnode;
		p->tail = newnode;
	}
	p->size++;


}


void Queuefrontpop(queue* p) {
	assert(p);
	assert(p->size > 0);
	if (p->head->next == NULL) {
		free(p->head);
		p->head = p->tail = NULL;
	}
	else {
		Qnode* next = p->head->next;
		free(p->head);
		p->head = next;
	}
	p->size--;
}


qdatatype Queuefrontdata(queue* p) {
	assert(p);
	assert(p->size > 0);
	return p->head->data;
}


qdatatype Queuebackdata(queue* p) {
	assert(p);
	assert(p->size > 0);
	return p->tail->data;
}


int Queuesize(queue* p) {
	assert(p);
	return p->size;
}


bool QueueEmpty(queue* p) {
	assert(p);
	return p->size == 0;
}